/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.primitives.cadmiumjdbc;

import java.sql.ResultSetMetaData;

import fr.x9c.cadmium.kernel.Block;
import fr.x9c.cadmium.kernel.CodeRunner;
import fr.x9c.cadmium.kernel.Fail;
import fr.x9c.cadmium.kernel.Primitive;
import fr.x9c.cadmium.kernel.PrimitiveProvider;
import fr.x9c.cadmium.kernel.Value;
import fr.x9c.cadmium.primitives.cadmium.Cadmium;

/**
 * This class provides the primitives related to the <tt>java.sql.ResultSetMetaData</tt> class.
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.1
 * @since 1.1
 */
@PrimitiveProvider
public final class ResultSetMetaDatas {

    /**
     * No instance of this class.
     */
    private ResultSetMetaDatas() {
    } // end empty constructor

    /**
     * Returns the catalog name for the column at the passed index.
     * @param ctxt context
     * @param resultSetMetaData result set metadata
     * @param column column index
     * @return the catalog name for the column at the passed index
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_resultsetmetadata_get_catalog_name(final CodeRunner ctxt,
                                                                       final Value resultSetMetaData,
                                                                       final Value column)
        throws Fail.Exception {
        try {
            final ResultSetMetaData rsm = (ResultSetMetaData) resultSetMetaData.asBlock().asCustom();
            final Block res =
                Block.createString(rsm.getCatalogName(column.asLong()));
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_resultsetmetadata_get_catalog_name(CodeRunner, Value, Value)'

    /**
     * Returns the fully-qualified class name for the column at the passed index.
     * @param ctxt context
     * @param resultSetMetaData result set metadata
     * @param column column index
     * @return the fully-qualified class name for the column at the passed index
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_resultsetmetadata_get_column_class_name(final CodeRunner ctxt,
                                                                            final Value resultSetMetaData,
                                                                            final Value column)
        throws Fail.Exception {
        try {
            final ResultSetMetaData rsm = (ResultSetMetaData) resultSetMetaData.asBlock().asCustom();
            final Block res =
                Block.createString(rsm.getColumnClassName(column.asLong()));
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_resultsetmetadata_get_column_class_name(CodeRunner, Value, Value)'

    /**
     * Returns the number of columns for the passed metadata.
     * @param ctxt context
     * @param resultSetMetaData result set metadata
     * @return the number of columns for the passed metadata
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_resultsetmetadata_get_column_count(final CodeRunner ctxt,
                                                                       final Value resultSetMetaData)
        throws Fail.Exception {
        try {
            final ResultSetMetaData rsm = (ResultSetMetaData) resultSetMetaData.asBlock().asCustom();
            return Value.createFromLong(rsm.getColumnCount());
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_resultsetmetadata_get_column_count(CodeRunner, Value)'

    /**
     * Returns the display size for the column at the passed index.
     * @param ctxt context
     * @param resultSetMetaData result set metadata
     * @param column column index
     * @return the display size for the column at the passed index
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_resultsetmetadata_get_column_display_size(final CodeRunner ctxt,
                                                                              final Value resultSetMetaData,
                                                                              final Value column)
        throws Fail.Exception {
        try {
            final ResultSetMetaData rsm = (ResultSetMetaData) resultSetMetaData.asBlock().asCustom();
            return Value.createFromLong(rsm.getColumnDisplaySize(column.asLong()));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_resultsetmetadata_get_column_display_size(CodeRunner, Value, Value)'

    /**
     * Returns the label for the column at the passed index.
     * @param ctxt context
     * @param resultSetMetaData result set metadata
     * @param column column index
     * @return the label for the column at the passed index
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_resultsetmetadata_get_column_label(final CodeRunner ctxt,
                                                                       final Value resultSetMetaData,
                                                                       final Value column)
        throws Fail.Exception {
        try {
            final ResultSetMetaData rsm = (ResultSetMetaData) resultSetMetaData.asBlock().asCustom();
            final Block res = Block.createString(rsm.getColumnLabel(column.asLong()));
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_resultsetmetadata_get_column_label(CodeRunner, Value, Value)'

    /**
     * Returns the name for the column at the passed index.
     * @param ctxt context
     * @param resultSetMetaData result set metadata
     * @param column column index
     * @return the name for the column at the passed index
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_resultsetmetadata_get_column_name(final CodeRunner ctxt,
                                                                      final Value resultSetMetaData,
                                                                      final Value column)
        throws Fail.Exception {
        try {
            final ResultSetMetaData rsm = (ResultSetMetaData) resultSetMetaData.asBlock().asCustom();
            final Block res = Block.createString(rsm.getColumnName(column.asLong()));
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_resultsetmetadata_get_column_name(CodeRunner, Value, Value)'

    /**
     * Returns the SQL type for the column at the passed index.
     * @param ctxt context
     * @param resultSetMetaData result set metadata
     * @param column column index
     * @return the SQL type for the column at the passed index
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_resultsetmetadata_get_column_type(final CodeRunner ctxt,
                                                                      final Value resultSetMetaData,
                                                                      final Value column)
        throws Fail.Exception {
        try {
            final ResultSetMetaData rsm = (ResultSetMetaData) resultSetMetaData.asBlock().asCustom();
            return Misc.encodeJDBCType(rsm.getColumnType(column.asLong()));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_resultsetmetadata_get_column_type(CodeRunner, Value, Value)'

    /**
     * Returns the type name for the column at the passed index.
     * @param ctxt context
     * @param resultSetMetaData result set metadata
     * @param column column index
     * @return the type name for the column at the passed index
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_resultsetmetadata_get_column_type_name(final CodeRunner ctxt,
                                                                           final Value resultSetMetaData,
                                                                           final Value column)
        throws Fail.Exception {
        try {
            final ResultSetMetaData rsm = (ResultSetMetaData) resultSetMetaData.asBlock().asCustom();
            final Block res = Block.createString(rsm.getColumnTypeName(column.asLong()));
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_resultsetmetadata_get_column_type_name(CodeRunner, Value, Value)'

    /**
     * Returns the precision for the column at the passed index.
     * @param ctxt context
     * @param resultSetMetaData result set metadata
     * @param column column index
     * @return the precision for the column at the passed index
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_resultsetmetadata_get_precision(final CodeRunner ctxt,
                                                                    final Value resultSetMetaData,
                                                                    final Value column)
        throws Fail.Exception {
        try {
            final ResultSetMetaData rsm = (ResultSetMetaData) resultSetMetaData.asBlock().asCustom();
            return Value.createFromLong(rsm.getPrecision(column.asLong()));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_resultsetmetadata_get_precision(CodeRunner, Value, Value)'

    /**
     * Returns the scale for the column at the passed index.
     * @param ctxt context
     * @param resultSetMetaData result set metadata
     * @param column column index
     * @return the scale for the column at the passed index
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_resultsetmetadata_get_scale(final CodeRunner ctxt,
                                                                final Value resultSetMetaData,
                                                                final Value column)
        throws Fail.Exception {
        try {
            final ResultSetMetaData rsm = (ResultSetMetaData) resultSetMetaData.asBlock().asCustom();
            return Value.createFromLong(rsm.getScale(column.asLong()));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_resultsetmetadata_get_scale(CodeRunner, Value, Value)'

    /**
     * Returns the schema name for the column at the passed index.
     * @param ctxt context
     * @param resultSetMetaData result set metadata
     * @param column column index
     * @return the schema name for the column at the passed index
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_resultsetmetadata_get_schema_name(final CodeRunner ctxt,
                                                                      final Value resultSetMetaData,
                                                                      final Value column)
        throws Fail.Exception {
        try {
            final ResultSetMetaData rsm = (ResultSetMetaData) resultSetMetaData.asBlock().asCustom();
            final Block res = Block.createString(rsm.getSchemaName(column.asLong()));
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_resultsetmetadata_get_schema_name(CodeRunner, Value, Value)'

    /**
     * Returns the table name for the column at the passed index.
     * @param ctxt context
     * @param resultSetMetaData result set metadata
     * @param column column index
     * @return the table name for the column at the passed index
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_resultsetmetadata_get_table_name(final CodeRunner ctxt,
                                                                     final Value resultSetMetaData,
                                                                     final Value column)
        throws Fail.Exception {
        try {
            final ResultSetMetaData rsm = (ResultSetMetaData) resultSetMetaData.asBlock().asCustom();
            final Block res = Block.createString(rsm.getTableName(column.asLong()));
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_resultsetmetadata_get_table_name(CodeRunner, Value, Value)'

    /**
     * Tests whether the column at the passed index is automatically incremented.
     * @param ctxt context
     * @param resultSetMetaData result set metadata
     * @param column column index
     * @return <tt>true</tt> if the column at the passed index is automatically incremented,
     *         <tt>false</tt> otherwise
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_resultsetmetadata_is_auto_increment(final CodeRunner ctxt,
                                                                        final Value resultSetMetaData,
                                                                        final Value column)
        throws Fail.Exception {
        try {
            final ResultSetMetaData rsm = (ResultSetMetaData) resultSetMetaData.asBlock().asCustom();
            return rsm.isAutoIncrement(column.asLong()) ? Value.TRUE : Value.FALSE;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_resultsetmetadata_is_auto_increment(CodeRunner, Value, Value)'

    /**
     * Tests whether the column at the passed index is case sensitive.
     * @param ctxt context
     * @param resultSetMetaData result set metadata
     * @param column column index
     * @return <tt>true</tt> if the column at the passed index is case sensitive,
     *         <tt>false</tt> otherwise
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_resultsetmetadata_is_case_sensitive(final CodeRunner ctxt,
                                                                        final Value resultSetMetaData,
                                                                        final Value column)
        throws Fail.Exception {
        try {
            final ResultSetMetaData rsm = (ResultSetMetaData) resultSetMetaData.asBlock().asCustom();
            return rsm.isCaseSensitive(column.asLong()) ? Value.TRUE : Value.FALSE;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_resultsetmetadata_is_case_sensitive(CodeRunner, Value, Value)'

    /**
     * Tests whether the column at the passed index contains currency values.
     * @param ctxt context
     * @param resultSetMetaData result set metadata
     * @param column column index
     * @return <tt>true</tt> if the column at the passed index contains currency values,
     *         <tt>false</tt> otherwise
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_resultsetmetadata_is_currency(final CodeRunner ctxt,
                                                                  final Value resultSetMetaData,
                                                                  final Value column)
        throws Fail.Exception {
        try {
            final ResultSetMetaData rsm = (ResultSetMetaData) resultSetMetaData.asBlock().asCustom();
            return rsm.isCurrency(column.asLong()) ? Value.TRUE : Value.FALSE;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_resultsetmetadata_is_currency(CodeRunner, Value, Value)'

    /**
     * Tests whether the column at the passed index is (doubtless) writable.
     * @param ctxt context
     * @param resultSetMetaData result set metadata
     * @param column column index
     * @return <tt>true</tt> if the column at the passed index is (doubtless) writable,
     *         <tt>false</tt> otherwise
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_resultsetmetadata_is_definitely_writable(final CodeRunner ctxt,
                                                                             final Value resultSetMetaData,
                                                                             final Value column)
        throws Fail.Exception {
        try {
            final ResultSetMetaData rsm = (ResultSetMetaData) resultSetMetaData.asBlock().asCustom();
            return rsm.isDefinitelyWritable(column.asLong()) ? Value.TRUE : Value.FALSE;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_resultsetmetadata_is_definitely_writable(CodeRunner, Value, Value)'

    /**
     * Tests whether the column at the passed index can contain <tt>null</tt> values.
     * @param ctxt context
     * @param resultSetMetaData result set metadata
     * @param column column index
     * @return <tt>true</tt> if the column at the passed index can contain <tt>null</tt> values,
     *         <tt>false</tt> otherwise
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_resultsetmetadata_is_nullable(final CodeRunner ctxt,
                                                                  final Value resultSetMetaData,
                                                                  final Value column)
        throws Fail.Exception {
        try {
            final ResultSetMetaData rsm = (ResultSetMetaData) resultSetMetaData.asBlock().asCustom();
            return Misc.encodeNullable(Misc.NullableKind.RESULT_SET_META_DATA,
                                       rsm.isNullable(column.asLong()));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_resultsetmetadata_is_nullable(CodeRunner, Value, Value)'

    /**
     * Tests whether the column at the passed index is read-only.
     * @param ctxt context
     * @param resultSetMetaData result set metadata
     * @param column column index
     * @return <tt>true</tt> if the column at the passed index is read-only,
     *         <tt>false</tt> otherwise
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_resultsetmetadata_is_read_only(final CodeRunner ctxt,
                                                                   final Value resultSetMetaData,
                                                                   final Value column)
        throws Fail.Exception {
        try {
            final ResultSetMetaData rsm = (ResultSetMetaData) resultSetMetaData.asBlock().asCustom();
            return rsm.isReadOnly(column.asLong()) ? Value.TRUE : Value.FALSE;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_resultsetmetadata_is_read_only(CodeRunner, Value, Value)'

    /**
     * Tests whether the column at the passed index is searchable.
     * @param ctxt context
     * @param resultSetMetaData result set metadata
     * @param column column index
     * @return <tt>true</tt> if the column at the passed index is searchable,
     *         <tt>false</tt> otherwise
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_resultsetmetadata_is_searchable(final CodeRunner ctxt,
                                                                    final Value resultSetMetaData,
                                                                    final Value column)
        throws Fail.Exception {
        try {
            final ResultSetMetaData rsm = (ResultSetMetaData) resultSetMetaData.asBlock().asCustom();
            return rsm.isSearchable(column.asLong()) ? Value.TRUE : Value.FALSE;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_resultsetmetadata_is_searchable(CodeRunner, Value, Value)'

    /**
     * Tests whether the values for the column at the passed index are signed.
     * @param ctxt context
     * @param resultSetMetaData result set metadata
     * @param column column index
     * @return <tt>true</tt> if the values for the column at the passed index are signed,
     *         <tt>false</tt> otherwise
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_resultsetmetadata_is_signed(final CodeRunner ctxt,
                                                                final Value resultSetMetaData,
                                                                final Value column)
        throws Fail.Exception {
        try {
            final ResultSetMetaData rsm = (ResultSetMetaData) resultSetMetaData.asBlock().asCustom();
            return rsm.isSigned(column.asLong()) ? Value.TRUE : Value.FALSE;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_resultsetmetadata_is_signed(CodeRunner, Value, Value)'

    /**
     * Tests whether the column at the passed index is writable.
     * @param ctxt context
     * @param resultSetMetaData result set metadata
     * @param column column index
     * @return <tt>true</tt> if the column at the passed index is writable,
     *         <tt>false</tt> otherwise
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_resultsetmetadata_is_writable(final CodeRunner ctxt,
                                                                  final Value resultSetMetaData,
                                                                  final Value column)
        throws Fail.Exception {
        try {
            final ResultSetMetaData rsm = (ResultSetMetaData) resultSetMetaData.asBlock().asCustom();
            return rsm.isWritable(column.asLong()) ? Value.TRUE : Value.FALSE;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_resultsetmetadata_is_writable(CodeRunner, Value, Value)'

} // end class 'ResultSetMetaDatas'
